#ifndef _XXDD_H
#define _XXDD_H

/*************************************************/
#define TimeDef         unsigned short
#define LineDef         unsigned char
#define END             ((TimeDef)-1)
#define LINE            ((__LINE__%(LineDef)-1)+1)  

#define me  (*cp)
#define TaskFun(TaskName)     TimeDef TaskName(C_##TaskName *cp) {switch(me.task.lc){default: 
#define Exit            do { me.task.lc=0; return END; }  while(0)
#define Restart           do { me.task.lc=0; return 0; }  while(0)
#define EndFun            } Exit; }

#define WaitX(ticks)      do { me.task.lc=LINE; return (ticks); case LINE:;} while(0)
#define WaitUntil(A)      do { while(!(A)) WaitX(0);} while(0)

#define UpdateTimer(TaskVar)    do { if((TaskVar.task.timer!=0)&&(TaskVar.task.timer!=END)) TaskVar.task.timer--; }  while(0)
#define RunTask(TaskName,TaskVar) do { if(TaskVar.task.timer==0) TaskVar.task.timer=TaskName(&(TaskVar)); }  while(0)

#define CallSub(SubTaskName,SubTaskVar)    do { WaitX(0);SubTaskVar.task.timer=SubTaskName(&(SubTaskVar));      \
                  if(SubTaskVar.task.timer!=END) return SubTaskVar.task.timer;} while(0)
    
/****************************************************************/
#define Class(type)         typedef struct C_##type C_##type; struct C_##type
Class(task)
{
  TimeDef timer;  //时间
  LineDef lc;  //行号
};


#endif
